package com.friend.campusnotice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.friend.campusnotice.adapter.CallLogAdapter;
import com.friend.campusnotice.adapter.ContactListAdapter;
import com.friend.campusnotice.bean.CallLog;
import com.friend.campusnotice.bean.Contacts;
import com.friend.campusnotice.util.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/25.
 */
public class CallLogFragment extends Fragment {

    private RecyclerView recyleView;
    private List<CallLog> callLogList;
    private CallLogAdapter mCallLogAdapter;
    private MyUtil myUtil = new MyUtil();
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCallLogAdapter.notifyDataSetChanged();
            // Toast.makeText(MainActivity.this,"加载完了",Toast.LENGTH_LONG).show();

        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         myUtil = new MyUtil();
        callLogList = myUtil.GetCall(getActivity());
        Log.i("haha","获取的长度"+callLogList.size());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.call_log_layout,container,false);


        initView(view);

        return view;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){

            callLogList.clear();
            callLogList.addAll(myUtil.GetCall(getActivity()));
            mCallLogAdapter.notifyDataSetChanged();

        }

    }

    private void initData() {
        new Thread(){
            @Override
            public void run() {
                callLogList.clear();
                MyUtil myUtil = new MyUtil();
                 callLogList = myUtil.GetCall(getActivity());
                handler.sendEmptyMessage(0);

            }
        }.start();
    }

    private void initView(View view) {
        recyleView = (RecyclerView) view.findViewById(R.id.call_log_rec);
        mCallLogAdapter = new CallLogAdapter(getActivity(),callLogList);
        mCallLogAdapter.setOnItemClickListener(new CallLogAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + callLogList.get(position).getPhoneNum()));
                startActivity(intent);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyleView.setLayoutManager(layoutManager);
        recyleView.setAdapter(mCallLogAdapter);

    }
}
